package cn.ebook.service.dao.impl;

import java.util.List;

import cn.ebook.entity.Ebook;
import cn.ebook.jdbc.dao.EbookDao;
import cn.ebook.jdbc.dao.impl.EbookImpl;
import cn.ebook.service.dao.EbookServiceDao;
import cn.ebook.util.PageBean;

public class EbookServiceImpl implements EbookServiceDao {
	/**
	 * 图书业务实现
	 * 
	 * @author 洋洋洋.
	 * @date 2018-7-27 3:31.
	 * 
	 */
	private EbookDao ebook;

	public EbookServiceImpl() {
		ebook = new EbookImpl();
	}

	/**
	 * @author 洋洋洋.
	 * @date 2018-7-27 3:41.
	 * 
	 */
	public PageBean<Ebook> findByCategoryAndLimit(Long categoryId, Integer nowPage, Integer pageSize) {
		try {
			// 查询总条数
			Integer totalData = ebook.countBooks(categoryId);
			PageBean page = new PageBean(nowPage, pageSize, totalData);
			List list = ebook.findEbookByCategoryByLimit(categoryId, page.getFirstIndex(), pageSize);
			page.setList(list);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 通过id查图书
	@Override
	public Ebook findEbookByid(Long ebookId) {
		try {
			Ebook book = ebook.findEbookByid(ebookId);
			return book;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 修改图书
	@Override
	public Boolean update(Ebook ebook) {
		try {
			int result = this.ebook.update(ebook);
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	// 添加图书
	@Override
	public Boolean add(Ebook ebook) {
		try {
			int result = this.ebook.add(ebook);
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 删除图书
	 */
	@Override
	public Boolean del(Long id) {
		try {
			int result = this.ebook.del(id);
			if (result == 1) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

}
